package org.csu.assemblyroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.assemblyroom.common.CommonResponse;
import org.csu.assemblyroom.entity.Room;
import org.csu.assemblyroom.persistence.RoomMapper;
import org.csu.assemblyroom.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public CommonResponse<List<Room>> getAllRooms() {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        List<Room> roomList = roomMapper.selectList(queryWrapper);
        return CommonResponse.createForSuccess("查询所有会议室", roomList);
    }
    @Override
    public CommonResponse<List<Room>> deleteRoomById(String roomnumber){
        QueryWrapper<Room> queryWrapper=new QueryWrapper<>();
        int i=roomMapper.deleteById(roomnumber);
        List<Room> roomList = roomMapper.selectList(queryWrapper);
        if(i==0){
            return CommonResponse.createForError("删除会议室失败！\n下一步请点击:/room/getAllroom ");
        }
        else return CommonResponse.createForSuccess("删除会议室成功 \n下一步请点击:/room/getAllroom",roomList);
    }

    @Override
    public CommonResponse<List<Room>> addRoom(Room room){
        QueryWrapper<Room> queryWrapper=new QueryWrapper<>();
        int i=roomMapper.insert(room);
        List<Room> roomList = roomMapper.selectList(queryWrapper);
        if(i==0){
            return CommonResponse.createForError("添加会议室失败！\n下一步请点击:/room/getAllroom");
        }
        else return CommonResponse.createForSuccess("添加会议室成功 \n下一步请点击:/room/getAllroom",roomList);
    }

    @Override
    public CommonResponse<Room> bookRoomById(String roomnumber){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomnumber",roomnumber);
        Room room=roomMapper.selectOne(queryWrapper);
        if (room==null){
            return CommonResponse.createForError("该会议室不存在！\n下一步请点击:/room/getAllroom");
        }
        else{
            room.setStatus(1);
            int i=roomMapper.update(room,queryWrapper);
            if (i==0){
                return CommonResponse.createForError("预约会议室失败！\n下一步请点击:/room/getAllroom");
            }
            else return CommonResponse.createForSuccess("预约会议室成功 \n下一步请点击:/room/getAllroom",room);
        }

    }
    @Override
    public CommonResponse<Room> getRoomById(String roomnumber){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomnumber",roomnumber);
        Room room=roomMapper.selectOne(queryWrapper);
        if(room==null){
            return CommonResponse.createForError("查找会议室失败，该会议室不存在 \n 下一步请点击:/room/getAllroom");
        }
        else return CommonResponse.createForSuccess("会议室查询成功！\n 下一步请点击:/room/getAllroom",room);
    }
}
